package atguigu.file;

import java.io.IOException;
import java.nio.file.*;
import java.util.Scanner;

/**
 * 批量修改文件的名字
 */
public class FileNameChangeUtils {
    public static void main(String[] args) {  
        FileCandidate candidate = new FileCandidate3();
        batchChangeName("D:\\Program Files\\JiJiDown\\Download\\黑马程序员匠心之作_C++教程从0到1入门编程,学习编程不再难",candidate);
    }

    public static void batchChangeName(String filePath,FileCandidate candidate){
        Path directory = Paths.get(filePath); // 替换为你的文件夹路径
        try {
            Files.walk(directory)
                .filter(Files::isRegularFile) // 只处理普通文件，不处理文件夹
                .forEach(path -> {
                    String fileName = path.getFileName().toString();
                    String fileSuffix = getFileSuffix(fileName);
                    System.out.println(fileSuffix);
                    // 候选文件名列表
                    String[] split = candidate.candidateFileNames(fileName);
                    String newFileName = "";
                    if(split.length>1){
                        System.out.println("---------------------------------");
                        // 手动选择文件名
                        int idx = selectNewName();
                        newFileName = split[idx];
                        if (hasExtension(fileName)) {
                            newFileName += "." + fileSuffix;
                        }
                    }else{
                        newFileName = split[0];
                    }
                    // 修改文件名
                    changeName(path, newFileName);
                    System.out.println("--------------完成-------------------");
                });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static boolean hasExtension(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return false; // 如果文件名是null或空字符串，则没有后缀
        }
        int dotIndex = fileName.lastIndexOf('.');
        return dotIndex > 0 && dotIndex < fileName.length() - 1;
    }

    private static String getFileSuffix(String fileName) {
        String fileExtension = "";
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex > 0) {
            fileExtension = fileName.substring(dotIndex + 1);
        }
        return fileExtension;
    }

    private static int selectNewName(){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请选择新的文件名：");
        int intValue = scanner.nextInt();
        System.out.println("你输入的整数是：" + intValue);
        scanner.close();
        return intValue;
    }

    private static void changeName(Path path, String newFileName) {
        Path newPath = path.resolveSibling(newFileName);
        try {
            Files.move(path, newPath);
            System.out.println("Renamed: " + path.getFileName() + " to " + newFileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}